﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SettlersEngine
{
    internal partial class RuntimeGrid
    {
        public unsafe void SaveDebugState()
        {
            System.IO.Directory.CreateDirectory(".\\DebugMap\\");

            System.Drawing.Bitmap target = new System.Drawing.Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // save path map
            {
                ImagePixelLock locked = new ImagePixelLock(target, false);

                using (locked)
                {
                    int* pixels = locked.Pixels;

                    for (int y = 0; y < Height; y++)
                    {
                        for (int x = 0; x < Width; x++)
                        {
                            int gray = 255;

                            if (!m_Cells[x, y].IsWalkable(null))
                                gray = 0;

                            *(pixels++) = unchecked((Int32)(0xFF000000 + (gray << 16) + (gray << 8) + gray));
                        }
                    }
                }

                target.Save(".\\DebugMap\\PathGrid.png");
            }


            // save height map 
            {
                ImagePixelLock locked = new ImagePixelLock(target, false);

                using (locked)
                {
                    int* pixels = locked.Pixels;

                    for (int y = 0; y < Height; y++)
                    {
                        for (int x = 0; x < Width; x++)
                        {
                            int gray = m_Cells[x, y].Height;

                            *(pixels++) = unchecked((Int32)(0xFF000000 + (gray << 16) + (gray << 8) + gray));
                        }
                    }
                }

                target.Save(".\\DebugMap\\HeightMap.png");
            }

            // save area map (in player colors)
            {
                String[] names = new String[] { 
                "AreaLockMap",
                "AutoMovableMap",
                "BuildingMap",
                "NatureMap",
                "UserMovableMap",
            };
                WallType[] walls = new WallType[] { 
                WallType.AreaLock,
                WallType.AutoMovable,
                WallType.Building,
                WallType.Nature,
                WallType.UserMovable,
            };

                for (int i = 0; i < names.Length; i++)
                {
                    ImagePixelLock locked = new ImagePixelLock(target, false);
                    WallType wallType = walls[i];

                    using (locked)
                    {
                        int* pixels = locked.Pixels;

                        for (int y = 0; y < Height; y++)
                        {
                            for (int x = 0; x < Width; x++)
                            {
                                System.Drawing.Color pixel = System.Drawing.Color.Green;

                                if (m_Cells[x, y].Wall == wallType)
                                    pixel = System.Drawing.Color.Black;
                                else switch (m_Cells[x, y].Type)
                                    {
                                        case GroundType.Water: pixel = System.Drawing.Color.Blue; break;
                                        case GroundType.Swamp: pixel = System.Drawing.Color.PaleVioletRed; break;
                                        case GroundType.Snow: pixel = System.Drawing.Color.GhostWhite; break;
                                        case GroundType.Mountain: pixel = System.Drawing.Color.Gray; break;
                                    }


                                *(pixels++) = pixel.ToArgb();
                            }
                        }
                    }

                    target.Save(".\\DebugMap\\" + names[i] + ".png");
                }
            }
        }

    }
}
